package com.hx.springboot1.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hx.springboot1.dao.ChooseDao;
import com.hx.springboot1.dao.CourseDao;
import com.hx.springboot1.dao.TeachDao;
import com.hx.springboot1.pojo.dto.StuDTO;
import com.hx.springboot1.pojo.entity.TabCourse;
import com.hx.springboot1.pojo.entity.TabUser;
import com.hx.springboot1.service.CourseService;
import com.hx.springboot1.utils.PinYinUtils;
import com.hx.springboot1.utils.ResultMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Author: hx
 * Date: 2021-05-24 14/53
 * Description: Nothing else
 */
@Service
@Slf4j
public class CourseServiceImpl implements CourseService {
    @Autowired
    CourseDao courseDao;
    @Autowired
    ChooseDao chooseDao;
    @Autowired
    TeachDao teachDao;
    @Override
    public PageInfo selectAllCourse(String searchName, String pageNum, String pageSize) {
        List<TabCourse> allCourse;
        int pn = 1;
        int ps = 10;
        if (!StringUtils.isEmpty(pageNum)) {
            pn = Integer.parseInt(pageNum);
        }
        if (!StringUtils.isEmpty(pageSize)) {
            ps = Integer.parseInt(pageSize);
        }
        //   PageHelper  要在查询之前使用
        PageHelper.startPage(pn, ps);
        if (StringUtils.isEmpty(searchName)) {
            allCourse = courseDao.selectAllCourse();
            log.info("用户{}点击courseList页面", SecurityContextHolder.getContext().getAuthentication().getName());
        } else {
            searchName = "%" + searchName + "%";
            allCourse = courseDao.selectCourseByName(searchName);
            log.info("{}查询了课程：{}的信息", SecurityContextHolder.getContext().getAuthentication().getName(), searchName);
        }
        PageInfo<TabCourse> pageInfo = new PageInfo<>(allCourse);
        return pageInfo;
    }

    @Override
    public ResultMsg insertOrUpdateCourse(Integer courseId,String courseName,Integer score,Integer term,String courseType,String testType,Integer courseHour,Integer practiceHour) {
        int effectRows = 0;
        if (courseId==null){
            /* 说明是新增 */
            effectRows += courseDao.insertCourse(courseName,score,term,courseType,testType,courseHour,practiceHour);
        }else {
            effectRows += courseDao.updateCourse(courseId,courseName,score,term,courseType,testType,courseHour,practiceHour);
        }
        if (effectRows>0){
            return new ResultMsg("操作成功！",true);
        }
        return new ResultMsg("操作失败！",false);
    }

    @Override
    public TabCourse selectCourseById(Integer courseId) {
        if (courseId!=null){
            return courseDao.selectCourseById(courseId);
        }
        return null;
    }

    @Override
    public ResultMsg delCourse(Integer CID) {
        int effectRows = 0;
        if (CID!=null){
            effectRows += courseDao.delCourse(CID);
            effectRows += teachDao.delTeachByCourseId(CID);
            effectRows += chooseDao.delTeachByCourseId(CID);
        }
        if (effectRows>0){
            return new ResultMsg("操作成功！",true);
        }
        return new ResultMsg("操作失败！",false);
    }

    @Override
    public List<TabCourse> selectCourseByYearId(Integer yearId) {
        if (yearId==null){
            return null;
        }
        List<TabCourse> tabCourses;
        if (yearId%2==0){
            /* 为偶数 说明是第二学期 */
            tabCourses = courseDao.selectCourseByTermId(2);
        }else {
            /* 否则为第一学期 */
            tabCourses = courseDao.selectCourseByTermId(1);
        }
        return tabCourses;
    }
}
